package com.cyan.learn.pattern.responsibility;

/**
 * 抽象处理者
 */
public abstract class Handler {

    // 下一个处理者
    private Handler nextHandler;

    // 可以处理假期的最大天数
    private int day;

    public Handler(Handler nextHandler, int day) {
        this.nextHandler = nextHandler;
        this.day = day;
    }

    public Handler(int day) {
        this.day = day;
    }

    public void setNextHandler(Handler nextHandler) {
        this.nextHandler = nextHandler;
    }

    // 各个处理者的具体实现：处理请求
    protected abstract void handleLeave(LeaveRequest leaveRequest);

    // 提交请求
    public final void submit(LeaveRequest leaveRequest) {
        if (leaveRequest.getDays() > day) {
            if (nextHandler != null) {
                this.nextHandler.submit(leaveRequest);
            } else {
                System.out.println("流程结束，超过时间规定，不同意");
            }
        } else {
            handleLeave(leaveRequest);
            System.out.println("流程结束");
        }
    }
}


